package com.sxz.mall.order.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxz.mall.common.exception.NoStockException;
import com.sxz.mall.order.entity.OrderEntity;
import com.sxz.mall.order.service.OrderService;
import com.sxz.mall.order.vo.OrderConfirmVo;
import com.sxz.mall.order.vo.OrderResponseVO;
import com.sxz.mall.order.vo.OrderSubmitVO;
import com.sxz.mall.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@Controller
public class PageController {

    @Autowired
    OrderService orderService;

    @GetMapping("/{page}.html")
    public String goPage(@PathVariable("page") String page) {
        System.out.println("请求进来了" + page);
        return page;
    }

    @GetMapping("/toTrade")
    public String toTrade(Model model) {

        OrderConfirmVo confirmVo = orderService.confirmOrder();
        model.addAttribute("confirmVo", confirmVo);
        return "confirm";
    }


    @PostMapping("/orderSubmit")
    public String orderSubmit(OrderSubmitVO vo, Model model) {
        System.out.println("提交订单接收到的数据：" + vo);
        //1、防重复校验, 订单提交都封装到service中处理
        OrderResponseVO responseVO = null;
        Integer code = null;
        try {
            responseVO = orderService.submitOrder(vo);
            code = responseVO.getCode();
        } catch (NoStockException e) {
            code = 2;
        }

        if (code == 0) {
            model.addAttribute("orderResponseVO", responseVO);
            // 下单成功
            return "pay";
        }
        System.out.println("库存返回码，不为0，codes：" + code);
        return "redirect:http://sxz.order.com/toTrade";

    }

    @GetMapping("/orderPay")
    public String orderPay(String orderSN) {

        System.out.println("支付需要的订单编号：" + orderSN);
        // todo 支付成功相关的逻辑

        return "list";
    }

    @GetMapping("/payOrder")
    // 相当于无需返回 (value = "/payOrder", produces = "text/html")
    public String payOrder(String orderSN) throws InterruptedException {

        PayVo payVo = orderService.getOrderPay(orderSN);

        // 拿到了支付时需要的信息， 金额，描述等等，进行支付
        // 支付跳转逻辑：封装的逻辑跳转到支付页面， 支付成功后，可以跳转到封装的配置的跳转路径
        // 即上面的地址： /orderPay
        System.out.println("10s后默认支付成功");
        Thread.sleep(10);
        System.out.println("支付成功...");

        // 订单支付状态修改
        OrderEntity orderEntity = orderService.getOne(new QueryWrapper<OrderEntity>()
                .eq("order_sn", orderSN));
        orderEntity.setStatus(1);
        orderService.update(orderEntity, new QueryWrapper<OrderEntity>().
                eq("order_sn", orderSN));

        // 更新库存数量

        // 购物车移除支付商品

        // 更新会员积分

        //以上涉及到分布式事务，seata 一致性方案 ----  mq消息

        return "list";
    }
}
